package com.graduation.cdm.funactivity.view.activityview;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.graduation.cdm.funactivity.MyApplication;
import com.graduation.cdm.funactivity.R;
import com.graduation.cdm.funactivity.bean.WeatherInfo;
import com.graduation.cdm.funactivity.util.CommonAdapter;
import com.graduation.cdm.funactivity.util.ViewHolder;
import com.graduation.cdm.funactivity.util.ownview.ListViewForScrollView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WeatherActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView now_we;
    private TextView we_desc;
    private TextView we_city;
    private TextView we_range;
    private TextView wind;
    private TextView humidity;
    private ImageView all_weather;
    private ImageView btn_more;
    private ImageView btn_less;
    private ImageView iv_bank;
    private RelativeLayout lv;


    List<WeatherInfo.Futuer> futuerList = new ArrayList<>();
    CommonAdapter<WeatherInfo.Futuer> futuerCommonAdapter;
    private ListViewForScrollView furture_lv;
    private TextView dressing_index;
    private TextView dressing_advice;
    private TextView uv_index;
    private TextView travel_index;
    private TextView exercise_index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather);

        initView();

        LocationManager locationManager;
        String serviceName = Context.LOCATION_SERVICE;
        locationManager = (LocationManager) getSystemService(serviceName); // 查找到服务信息
        //locationManager.setTestProviderEnabled("gps", true);
        Toast.makeText(getApplicationContext(), "正在获取.....", Toast.LENGTH_SHORT).show();

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300000, 8, mLocationListener01);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 300000, 8, mLocationListener01);

    }

    private void initView(){
        iv_bank = ((ImageView) findViewById(R.id.iv_bank));
        now_we = ((TextView) findViewById(R.id.now_we));
        we_desc = ((TextView) findViewById(R.id.we_desc));
        we_city = ((TextView) findViewById(R.id.we_city));
        we_range = ((TextView) findViewById(R.id.we_range));
        wind = ((TextView) findViewById(R.id.wind));
        humidity = ((TextView) findViewById(R.id.humidity));
        all_weather = ((ImageView) findViewById(R.id.all_weather));
        lv = ((RelativeLayout) findViewById(R.id.lv));
        dressing_index = ((TextView) findViewById(R.id.dressing_index));
        dressing_advice = ((TextView) findViewById(R.id.dressing_advice));
        uv_index = ((TextView) findViewById(R.id.uv_index));
        travel_index = ((TextView) findViewById(R.id.travel_index));
        exercise_index = ((TextView) findViewById(R.id.exercise_index));
        furture_lv = ((ListViewForScrollView) findViewById(R.id.furture_lv));

        btn_more = ((ImageView) findViewById(R.id.btn_more));
        btn_less = ((ImageView) findViewById(R.id.btn_less));

        iv_bank.setOnClickListener(this);
        btn_more.setOnClickListener(this);
        btn_less.setOnClickListener(this);

    }

    public final LocationListener mLocationListener01 = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            updateToNewLocation(location);
        }

        @Override
        public void onProviderDisabled(String provider) {
            updateToNewLocation(null);
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };

    private Location updateToNewLocation(Location location) {
        System.out.println("--------zhixing--2--------");
        String latLongString;
        double lat = 0;
        double lng = 0;

        if (location != null) {
            lat = location.getLatitude();
            lng = location.getLongitude();
            latLongString = "纬度:" + lat + "\n经度:" + lng;
            System.out.println("经度：" + lng + "纬度：" + lat);
            getData(location);
        } else {
            latLongString = "无法获取地理信息，请稍后...";
            Toast.makeText(getApplicationContext(), latLongString, Toast.LENGTH_SHORT).show();
            latLongString="";
        }
        if (lat != 0) {
            System.out.println("--------反馈信息----------" + String.valueOf(lat));
        }

        //Toast.makeText(getApplicationContext(), latLongString, Toast.LENGTH_SHORT).show();

        return location;

    }

    public void getData(final Location location) {
        String url = "http://v.juhe.cn/weather/geo?format=2&key=967a3459aecf73c544203d1469115990";
        StringRequest weatherRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                System.out.println("onSuccess==" + s.trim());
                Gson gson = new Gson();
                WeatherInfo weatherInfo = gson.fromJson(s, WeatherInfo.class);
                System.out.println("weatherInfo" + weatherInfo);
                if (futuerList == null) {
                    futuerList.addAll(weatherInfo.result.future);
                    futuerList.remove(0);
                } else {
                    futuerList.clear();
                    futuerList.addAll(weatherInfo.result.future);
                    futuerList.remove(0);
                }
                System.out.println("futuerList0000" + weatherInfo.result.future);
                System.out.println("futuerList" + futuerList);

                WeatherInfo.Sk sk = weatherInfo.result.sk;
                System.out.println("sk" + sk);
                WeatherInfo.Today today = weatherInfo.result.today;
                System.out.println("today" + today);
                now_we.setText(sk.temp + "°");
                we_desc.setText(today.weather);
                we_city.setText(today.city);
                we_range.setText(today.temperature);
                wind.setText("风力：" + today.wind);
                humidity.setText("湿度：" + sk.humidity);
                dressing_index.setText(today.dressing_index);
                dressing_advice.setText(today.dressing_advice);
                uv_index.setText(today.uv_index);
                travel_index.setText(today.travel_index);
                exercise_index.setText(today.exercise_index);
                iconToday(all_weather, today.weather);
                ininData();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map=new HashMap<>();
                map.put("lon",location.getLongitude() + "");
                map.put("lat",location.getLatitude() + "");
                return map;
            }
        };
        MyApplication.getRequestQueue().add(weatherRequest);
    }

    public void ininData() {
        futuerCommonAdapter = new CommonAdapter<WeatherInfo.Futuer>(getApplicationContext(), futuerList, R.layout.further_day_item) {
            @Override
            public void convert(ViewHolder viewHolder, WeatherInfo.Futuer futuer, int position) {
                TextView week = viewHolder.getViewById(R.id.daily_time);
                TextView timp = viewHolder.getViewById(R.id.daily_tmphigh);
                TextView date_week = viewHolder.getViewById(R.id.date_week);
                TextView daily_tv_weather = viewHolder.getViewById(R.id.daily_tv_weather);
                TextView daily_wind = viewHolder.getViewById(R.id.daily_wind);
                String year = futuer.date.substring(0,4);
                String month = futuer.date.substring(4,6);
                String date = futuer.date.substring(6,8);
                week.setText(year+"-"+month+"-"+date);
                timp.setText(futuer.temperature);
                date_week.setText(futuer.week);
                daily_tv_weather.setText(futuer.weather);
                daily_wind.setText(futuer.wind);

                ImageView icon = viewHolder.getViewById(R.id.daily_weather);
                iconToday(icon, futuer.weather);
            }
        };
        furture_lv.setAdapter(futuerCommonAdapter);
    }

    public void iconToday(ImageView allWeather, String weather) {
        if (weather.equals("晴")) {
            allWeather.setImageResource(R.mipmap.sun_small);
        } else if (weather.equals("多云转晴") || weather.equals("晴转多云")) {
            allWeather.setImageResource(R.mipmap.suncloudy_small);
        } else if (weather.equals("多云")) {
            allWeather.setImageResource(R.mipmap.cloudy_small);
        } else if (weather.equals("阴")) {
            allWeather.setImageResource(R.mipmap.cloudy_small);
        } else if (weather.contains("风")) {
            allWeather.setImageResource(R.mipmap.wind_small);
        } else if (weather.contains("雷阵雨")) {
            allWeather.setImageResource(R.mipmap.thunder_small);
        } else if (weather.contains("阵雨")) {
            allWeather.setImageResource(R.mipmap.shower_small);
        } else if (weather.contains("雨")) {
            allWeather.setImageResource(R.mipmap.rain_small);
        } else if (weather.contains("雪")) {
            allWeather.setImageResource(R.mipmap.snow_small);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_bank:
                finish();
                break;
            case R.id.btn_more:
                lv.setVisibility(View.VISIBLE);
                btn_more.setVisibility(View.GONE);
                btn_less.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_less:
                lv.setVisibility(View.GONE);
                btn_more.setVisibility(View.VISIBLE);
                btn_less.setVisibility(View.GONE);
                break;
        }
    }
}
